package jym.tan.movielibrary.component.listener;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import jym.tan.movielibrary.component.ui.timer.MovieTitleConfirmTimer;
import jym.tan.movielibrary.factory.ComponentFactory;
import jym.tan.movielibrary.factory.TimerFactory;
import jym.tan.movielibrary.logger.LibraryLogger;

public class MovieTitleConfirmDialogWindowListener implements WindowListener {

	public void windowOpened(WindowEvent windowEvent) {
		if(windowEvent.getSource() == ComponentFactory.getMovieTitleConfirmDialog()) {
			toggleStartStopMovieTitleConfirmTimer(false);			
		}
	}

	public void windowClosing(WindowEvent windowEvent) {
				
	}

	public void windowClosed(WindowEvent windowEvent) {
		if(windowEvent.getSource() == ComponentFactory.getMovieTitleConfirmDialog()) {
			toggleStartStopMovieTitleConfirmTimer(true);
		}
	}

	public void windowIconified(WindowEvent windowEvent) {

	}

	public void windowDeiconified(WindowEvent windowEvent) {

	}

	public void windowActivated(WindowEvent windowEvent) {
		if(windowEvent.getSource() == ComponentFactory.getMovieTitleConfirmDialog()) {
			toggleStartStopMovieTitleConfirmTimer(false);			
		}
	}

	public void windowDeactivated(WindowEvent windowEvent) {
		
	}
	
	private void toggleStartStopMovieTitleConfirmTimer(Boolean stop) {
		MovieTitleConfirmTimer movieTitleConfirmTimer = TimerFactory.getMovieTitleConfirmTimer();
		if(stop) {
			if(movieTitleConfirmTimer.isRunning()) {
				movieTitleConfirmTimer.stop();
				LibraryLogger.stopTimerMessage(movieTitleConfirmTimer.getClass().getSimpleName());
			}
		} else {
			if(!movieTitleConfirmTimer.isRunning()) {
				movieTitleConfirmTimer.start();
				LibraryLogger.startTimerMessage(movieTitleConfirmTimer.getClass().getSimpleName());
			}
		}		
	}
}
